package stark.app.function.nfc;

import android.text.TextUtils;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.nfc.lib.NfcFormatActivity;
import com.stark.nfc.lib.NfcOperateRecActivity;
import com.stark.nfc.lib.NfcReadActivity;
import com.stark.nfc.lib.NfcWriteActivity;
import com.stark.nfc.lib.model.NfcOpType;
import com.stark.nfc.lib.model.NfcTagAccessor;
import com.stark.nfc.lib.model.NfcUtil;
import com.stark.nfc.lib.model.db.NfcDbManager;
import com.stark.nfc.lib.model.db.NfcOperateBean;

import java.util.List;

import stark.app.R;
import stark.app.databinding.ActivityNfcDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;

/**
 * Author：daizhongbin on 2023/3/3
 * Email：1027154338@qq.com
 * Desc:
 */
public class NfcDemoActivity extends BaseNoModelActivity<ActivityNfcDemoBinding> {
    @Override
    protected int onCreate() {
        return R.layout.activity_nfc_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.btnRead.setOnClickListener(v -> {
            handleClick(() -> {
                startActivity(NfcReadActivity.class);
            });
        });
        mDataBinding.btnWrite.setOnClickListener(v -> {
            handleClick(() -> {
                startActivity(NfcWriteActivity.class);
            });
        });
        mDataBinding.btnWriteRead.setOnClickListener(v -> {
            handleClick(() -> {
                handleClickWriteRead();
            });
        });
        mDataBinding.btnFormat.setOnClickListener(v -> {
            handleClick(() -> {
                startActivity(NfcFormatActivity.class);
            });
        });
    }

    @Override
    protected void initData() {

    }

    private void handleClickWriteRead() {
        /**
         * 写卡界面需继承NfcBaseActivity。当然也可以不继承该Activity，
         * 但是NfcBaseActivity中的相关操作代码需自己写到Activity中去
         */

        /**
         * 下面我随便取一个，实际界面中要实现选取一条读卡记录的操作界面
         */
        List<NfcOperateBean> readBeans = NfcDbManager.getInstance().nfcOperateDao().getByType(NfcOpType.READ.ordinal());
        if (readBeans == null || readBeans.size() == 0) {
            //还没有读卡记录
            ToastUtils.showShort("还未有读卡记录");
            return;
        }
        NfcOperateBean readBean = readBeans.get(0);
        if (TextUtils.isEmpty(readBean.data)) {
            ToastUtils.showShort("该读卡记录之前未读到有效数据");
            return;
        }
        NfcTagAccessor.instance().write(readBean, ret -> {
            if (!TextUtils.isEmpty(ret.msg)) {
                ToastUtils.showShort(ret.msg);
            }
            if (ret.data != null) {
                NfcOperateRecActivity.start(this, NfcOpType.WRITE);
            }
        });
    }

    private void handleClick(ICallback callback) {
        if (!NfcUtil.isSupportNfc()) {
            ToastUtils.showShort("当前设备不支持nfc功能");
            return;
        }
        if (!NfcUtil.isNfcOpened()) {
            //nfc开关没有打开，则跳去nfc开关界面引导用户打开nfc开关
            NfcUtil.goNfcSetting(this);
            return;
        }
        if (callback != null) {
            callback.executeAction();
        }
    }

    private interface ICallback {

        void executeAction();

    }
}
